package com.learn.Chapter1_1_1.Controller;

import com.entity.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
* @Author: zhaoben
* @Date: 2016/12/16 21:40
* @Desc: 用户相关操作
*/
@RestController
@RequestMapping("/user")
public class UserController {

    Map<String,User> userDB = Collections.synchronizedMap(new HashMap<String, User>());

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
    public User getUserById(@PathVariable("id") String id){
        return userDB.get(id);
    }

    @RequestMapping(value="/save", method=RequestMethod.POST, consumes = "application/json")
    public String saveUser(@RequestBody List<User> users){
        for (User user: users) {
            userDB.put(user.getId(),user);
        }
        return "success";
    }

    @RequestMapping(value="/",method = RequestMethod.GET, produces = "application/json")
    public List<User> getAll(){
        return new ArrayList<>(userDB.values());
    }
}
